<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebP动图转PNG工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            max-width: 600px;
            width: 100%;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 2em;
            font-weight: 700;
        }

        .upload-area {
            border: 2px dashed #667eea;
            border-radius: 15px;
            padding: 40px 20px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            background: rgba(102, 126, 234, 0.05);
        }

        .upload-area:hover {
            border-color: #764ba2;
            background: rgba(118, 75, 162, 0.1);
            transform: translateY(-2px);
        }

        .upload-area.dragover {
            border-color: #764ba2;
            background: rgba(118, 75, 162, 0.15);
            transform: scale(1.02);
        }

        .upload-icon {
            font-size: 48px;
            margin-bottom: 15px;
            color: #667eea;
        }

        .upload-text {
            color: #666;
            font-size: 16px;
            margin-bottom: 10px;
        }

        .upload-hint {
            color: #999;
            font-size: 14px;
        }

        #fileInput {
            display: none;
        }

        .preview-section {
            display: none;
            margin-top: 30px;
        }

        .preview-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }

        .preview-item {
            text-align: center;
            padding: 20px;
            background: rgba(255, 255, 255, 0.5);
            border-radius: 15px;
            border: 1px solid rgba(255, 255, 255, 0.3);
        }

        .preview-item h3 {
            color: #333;
            margin-bottom: 15px;
            font-size: 1.1em;
        }

        .preview-item img {
            max-width: 100%;
            max-height: 200px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .convert-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: block;
            margin: 20px auto;
            min-width: 200px;
        }

        .convert-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
        }

        .convert-btn:active {
            transform: translateY(0);
        }

        .convert-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .download-btn {
            background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: none;
            margin: 20px auto;
            min-width: 200px;
        }

        .download-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(72, 187, 120, 0.4);
        }

        .error-message {
            color: #e53e3e;
            text-align: center;
            margin-top: 15px;
            padding: 15px;
            background: rgba(229, 62, 62, 0.1);
            border-radius: 10px;
            border: 1px solid rgba(229, 62, 62, 0.2);
            display: none;
        }

        .success-message {
            color: #38a169;
            text-align: center;
            margin-top: 15px;
            padding: 15px;
            background: rgba(56, 161, 105, 0.1);
            border-radius: 10px;
            border: 1px solid rgba(56, 161, 105, 0.2);
            display: none;
        }

        .loading {
            display: none;
            text-align: center;
            margin-top: 20px;
        }

        .loading-spinner {
            width: 40px;
            height: 40px;
            border: 3px solid rgba(102, 126, 234, 0.3);
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        @media (max-width: 640px) {
            .preview-grid {
                grid-template-columns: 1fr;
            }
            
            .container {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>WebP动图转PNG工具</h1>
        
        <div class="upload-area" onclick="document.getElementById('fileInput').click()">
            <div class="upload-icon">📎</div>
            <div class="upload-text">点击选择或拖拽WebP动图文件</div>
            <div class="upload-hint">支持 .webp 格式的动图文件</div>
        </div>
        
        <input type="file" id="fileInput" accept=".webp,image/webp">
        
        <div class="preview-section" id="previewSection">
            <div class="preview-grid">
                <div class="preview-item">
                    <h3>原始WebP动图</h3>
                    <img id="originalImage" alt="原始图片">
                </div>
                <div class="preview-item">
                    <h3>转换后的PNG</h3>
                    <img id="convertedImage" alt="转换后图片">
                </div>
            </div>
            
            <button class="convert-btn" id="convertBtn">🔄 转换为PNG</button>
            <button class="download-btn" id="downloadBtn">📥 下载PNG图片</button>
        </div>
        
        <div class="loading" id="loading">
            <div class="loading-spinner"></div>
            <div>正在处理中...</div>
        </div>
        
        <div class="error-message" id="errorMessage"></div>
        <div class="success-message" id="successMessage"></div>
    </div>

    <script>
        const fileInput = document.getElementById('fileInput');
        const uploadArea = document.querySelector('.upload-area');
        const previewSection = document.getElementById('previewSection');
        const originalImage = document.getElementById('originalImage');
        const convertedImage = document.getElementById('convertedImage');
        const convertBtn = document.getElementById('convertBtn');
        const downloadBtn = document.getElementById('downloadBtn');
        const errorMessage = document.getElementById('errorMessage');
        const successMessage = document.getElementById('successMessage');
        const loading = document.getElementById('loading');

        let currentFile = null;
        let convertedBlob = null;

        // 拖拽上传
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });

        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });

        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                handleFile(files[0]);
            }
        });

        // 文件选择
        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                handleFile(e.target.files[0]);
            }
        });

        // 处理文件
        function handleFile(file) {
            hideMessages();
            
            if (!file.type.includes('webp')) {
                showError('请选择WebP格式的文件');
                return;
            }

            currentFile = file;
            
            // 显示原始图片
            const reader = new FileReader();
            reader.onload = (e) => {
                originalImage.src = e.target.result;
                previewSection.style.display = 'block';
                convertBtn.disabled = false;
                convertedImage.style.display = 'none';
                downloadBtn.style.display = 'none';
            };
            reader.readAsDataURL(file);
        }

        // 转换按钮点击
        convertBtn.addEventListener('click', () => {
            if (!currentFile) return;
            
            convertToPNG();
        });

        // 转换为PNG
        function convertToPNG() {
            showLoading();
            hideMessages();
            
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            const img = new Image();
            
            img.onload = () => {
                // 设置canvas尺寸
                canvas.width = img.naturalWidth;
                canvas.height = img.naturalHeight;
                
                // 绘制图片到canvas
                ctx.drawImage(img, 0, 0);
                
                // 转换为PNG
                canvas.toBlob((blob) => {
                    convertedBlob = blob;
                    
                    // 显示转换后的图片
                    const url = URL.createObjectURL(blob);
                    convertedImage.src = url;
                    convertedImage.style.display = 'block';
                    
                    // 显示下载按钮
                    downloadBtn.style.display = 'block';
                    
                    hideLoading();
                    showSuccess('转换成功！点击下载按钮保存PNG图片');
                }, 'image/png', 1.0);
            };
            
            img.onerror = () => {
                hideLoading();
                showError('图片加载失败，请检查文件格式');
            };
            
            // 使用原始文件创建图片URL
            const url = URL.createObjectURL(currentFile);
            img.src = url;
        }

        // 下载按钮点击
        downloadBtn.addEventListener('click', () => {
            if (!convertedBlob) return;
            
            const url = URL.createObjectURL(convertedBlob);
            const a = document.createElement('a');
            a.href = url;
            a.download = currentFile.name.replace(/\.[^/.]+$/, '') + '.png';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        });

        // 工具函数
        function showError(message) {
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }

        function showSuccess(message) {
            successMessage.textContent = message;
            successMessage.style.display = 'block';
        }

        function hideMessages() {
            errorMessage.style.display = 'none';
            successMessage.style.display = 'none';
        }

        function showLoading() {
            loading.style.display = 'block';
        }

        function hideLoading() {
            loading.style.display = 'none';
        }
    </script>
</body>
</html>